package com.kky.pp.base

import android.content.Intent
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.custom.base.mvp.MVPBaseActivity
import com.kky.pp.base.manager.ActivityForResultManager
import com.kky.pp.callback.RegisterForActivityResultCall

abstract class TitleMvpBaseActivity<V : TitleBaseView, T : TitleBasePresenterImpl<V>, VB : ViewBinding>(inflate: (LayoutInflater) -> VB)
    : MVPBaseActivity<V, T, VB>(inflate), TitleBaseView {
    override val getTitle: String = setTitle();
    override val getRightTitle: String = getRightTitle();
    override val getLeftResID: Int = setLeftResID()
    override val showBack: Boolean = isShowBack()
    open fun setTitle(): String{
        return ""
    }
    open fun getRightTitle(): String{
        return ""
    }
    open fun setLeftResID(): Int{
        return 0
    }
    open fun isShowBack(): Boolean{
        return true
    }

    fun registerActivityResult (
        intent: Intent? = null,
        registerForActivityResultCall: RegisterForActivityResultCall? = null
    ) : AppCompatActivity {
        intent?.putExtra(RegisterForActivityResultCall.ACTIVITY_NAME,componentName.className)
        ActivityForResultManager.registerActivity(componentName.className, registerForActivityResultCall)
        return this
    }

    fun setResult(intent: Intent, resultCode: Int = -1) {
        val lastActivityKey = intent.getStringExtra(RegisterForActivityResultCall.ACTIVITY_NAME)
        val registerForActivityResultCall = ActivityForResultManager.getActivityResultCall(lastActivityKey)
        registerForActivityResultCall.result(intent,resultCode)
        ActivityForResultManager.unRegisterActivity(lastActivityKey)
    }
}